package com.ad_hccm.service.impl.Week;

import com.ad_hccm.dao.Appendix.AEDao;
import com.ad_hccm.dao.Week.AdverseReactionRecordDao;
import com.ad_hccm.domain.Appendix.AE;
import com.ad_hccm.domain.Appendix.CompletionStatus;
import com.ad_hccm.domain.Appendix.SAE;
import com.ad_hccm.domain.Week.AdverseReactionRecord;
import com.ad_hccm.service.Appendix.AEService;
import com.ad_hccm.service.Appendix.CompletionStatusService;
import com.ad_hccm.service.Appendix.SAEService;
import com.ad_hccm.service.Week.AdverseReactionRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * TODO 不良反应记录
 */
@Service
public class AdverseReactionRecordServiceImpl implements AdverseReactionRecordService {
    @Autowired
    private AdverseReactionRecordDao dao;
    @Autowired
    private AEService aeService;
    @Autowired
    private SAEService saeService;
    @Autowired
    private CompletionStatusService completionStatusService;

    @Override
    public boolean save(AdverseReactionRecord domain) {
        return dao.save(domain);
    }

    @Override
    public boolean update(AdverseReactionRecord domain) {
        if(domain.getIsAdverseEvent()==0){
            AE ae = new AE();
            ae.setId(domain.getId());
            ae.setWeek(domain.getWeek());
            if(aeService.getWeek(ae)!=null) aeService.delete(ae);
        }else if(domain.getIsAdverseEvent()==1){
            AE ae = new AE();
            ae.setId(domain.getId());
            ae.setWeek(domain.getWeek());
            if(aeService.getWeek(ae)==null) {
                aeService.save(ae);
            }
        }
        if(domain.getIsSeriousAdverseEvent()==0){
            SAE sae = new SAE();
            sae.setId(domain.getId());
            sae.setWeek(domain.getWeek());
            if(saeService.getWeek(sae)!=null)saeService.delete(sae);
        }else {
            SAE sae = new SAE();
            sae.setId(domain.getId());
            sae.setWeek(domain.getWeek());
            if(saeService.getWeek(sae)==null)saeService.save(sae);
        }
        if(domain.getIsExitExperiment()==0){
            //没有比这更后的且该删除的数据存在
            if(getBigWeek(domain).size()==0 && completionStatusService.getById(domain.getId())!=null)
                completionStatusService.delete(domain.getId());
        }else if(domain.getIsExitExperiment()==1){
            CompletionStatus CS = new CompletionStatus();
            CS.setId(domain.getId());
            deleteBigWeek(domain);  //说明到这就结束了，把后面的ae sae 和这个表的大的week都删了
            if(completionStatusService.getById(domain.getId())==null)completionStatusService.save(CS);
        }
        return dao.update(domain);
    }

    @Override
    public List<AdverseReactionRecord> getById(String id) {
        return dao.getById(id);
    }

    @Override
    public List<AdverseReactionRecord> getBigWeek(AdverseReactionRecord domain) {
        return dao.getBigWeek(domain);
    }

    @Override
    public List<AdverseReactionRecord> getAll() {
        return dao.getAll();
    }

    @Override
    public boolean delete(AdverseReactionRecord domain) {
        return dao.delete(domain);
    }

    @Override
    public boolean deleteBigWeek(AdverseReactionRecord domain) {
        AE ae = new AE();
        ae.setId(domain.getId());
        ae.setWeek(domain.getWeek());
        aeService.deleteBigWeek(ae);
        SAE sae = new SAE();
        sae.setId(domain.getId());
        sae.setWeek(domain.getWeek());
        saeService.deleteBigWeek(sae);
        return dao.deleteBigWeek(domain);
    }
}
